/*=============================================================================

  Library: CFS

  Copyright (c) German Cancer Research Center,
    Division of Medical and Biological Informatics

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=============================================================================*/

#include <QStringList>

#include "CFSPlugin.h"
#include "CFSPlugin_p.h"
#include "CFSPluginContext.h"
#include "CFSPluginContext_p.h"
#include "CFSPluginFrameworkUtil_p.h"
#include "CFSPluginArchive_p.h"
#include "CFSPluginFrameworkContext_p.h"
#include "CFSServices_p.h"
#include "CFSUtils.h"

//----------------------------------------------------------------------------
CFSPlugin::CFSPlugin()
: d_ptr(0)
{

}

//----------------------------------------------------------------------------
void CFSPlugin::init(CFSPluginPrivate* dd)
{
  if (d_ptr) throw CFSIllegalStateException("CFSPlugin already initialized");
  d_ptr = dd;
}

//----------------------------------------------------------------------------
void CFSPlugin::init(const QWeakPointer<CFSPlugin>& self,
                     CFSPluginFrameworkContext* fw,
                     QSharedPointer<CFSPluginArchive> pa)
{
  if (d_ptr) throw CFSIllegalStateException("CFSPlugin already initialized");
  d_ptr = new CFSPluginPrivate(self, fw, pa);
}

//----------------------------------------------------------------------------
CFSPlugin::~CFSPlugin()
{
  delete d_ptr;
}

//----------------------------------------------------------------------------
CFSPlugin::State CFSPlugin::getState() const
{
  Q_D(const CFSPlugin);
  return d->state;
}

//----------------------------------------------------------------------------
void CFSPlugin::start(const StartOptions& options)
{
  Q_D(CFSPlugin);

  if (d->state == UNINSTALLED)
  {
    throw CFSIllegalStateException("CFSPlugin is uninstalled");
  }

  // Initialize the activation; checks initialization of lazy
  // activation.

  //TODO 1: If activating or deactivating, wait a litle
  // we don't use mutliple threads to start plugins for now
  //waitOnActivation(lock, "CFSPlugin::start", false);

  //2: start() is idempotent, i.e., nothing to do when already started
  if (d->state == ACTIVE)
  {
    return;
  }

  //3: Record non-transient start requests.
  if ((options & START_TRANSIENT) == 0)
  {
    d->setAutostartSetting(options);
  }

  //4: Resolve plugin (if needed)
  d->getUpdatedState();

  //5: Eager?
  if ((options & START_ACTIVATION_POLICY) && !d->eagerActivation )
  {
    if (STARTING == d->state) return;
    d->state = STARTING;
    d->pluginContext.reset(new CFSPluginContext(this->d_func()));
    CFSPluginEvent pluginEvent(CFSPluginEvent::LAZY_ACTIVATION, d->q_ptr);
    d->fwCtx->listeners.emitPluginChanged(pluginEvent);
  }
  else
  {
    d->finalizeActivation();
  }
}

//----------------------------------------------------------------------------
void CFSPlugin::stop(const StopOptions& options)
{
  Q_D(CFSPlugin);

  const CFSRuntimeException* savedException = 0;

  //1:
  if (d->state == UNINSTALLED)
  {
    throw CFSIllegalStateException("CFSPlugin is uninstalled");
  }

  // 2: If an operation is in progress, wait a little
  d->waitOnOperation(&d->operationLock, "Plugin::stop", false);

  //3:
  if ((options & STOP_TRANSIENT) == 0)
  {
    d->ignoreAutostartSetting();
  }

  switch (d->state)
  {
  case INSTALLED:
  case RESOLVED:
  case STOPPING:
  case UNINSTALLED:
    //4:
    return;

  case ACTIVE:
  case STARTING: // Lazy start...
    savedException = d->stop0();
    break;
  };

  if (savedException != 0)
  {
    if (const CFSPluginException* pluginExc = dynamic_cast<const CFSPluginException*>(savedException))
    {
      CFSPluginException pe(*pluginExc);
      delete savedException;
      throw pe;
    }
    else
    {
      CFSRuntimeException re(*savedException);
      delete savedException;
      throw re;
    }
  }
}

//----------------------------------------------------------------------------
void CFSPlugin::update(const QUrl& updateLocation)
{
  Q_D(CFSPlugin);
  CFSPluginPrivate::Locker sync(&d->operationLock);
  const bool wasActive = d->state == ACTIVE;

  switch (d->getUpdatedState_unlocked())
  {
  case ACTIVE:
    stop(STOP_TRANSIENT);
    // Fall through
  case RESOLVED:
  case INSTALLED:
    // Load new plugin
    //secure.callUpdate0(this, in, wasActive);
    d->update0(updateLocation, wasActive);
    break;
  case STARTING:
    // Wait for RUNNING state, this doesn't happen now
    // since we are synchronized.
    throw CFSIllegalStateException("Plugin is in STARTING state");
  case STOPPING:
    // Wait for RESOLVED state, this doesn't happen now
    // since we are synchronized.
    throw CFSIllegalStateException("Plugin is in STOPPING state");
  case UNINSTALLED:
    throw CFSIllegalStateException("Plugin is in UNINSTALLED state");
  }
}

//----------------------------------------------------------------------------
void CFSPlugin::uninstall()
{
  Q_D(CFSPlugin);
  {
    CFSPluginPrivate::Locker sync(&d->operationLock);

    if (d->archive)
    {
      try
      {
        d->archive->setStartLevel(-2); // Mark as uninstalled
      }
      catch (...)
      { }
    }

    switch (d->state)
    {
    case UNINSTALLED:
      throw CFSIllegalStateException("Plugin is in UNINSTALLED state");

    case STARTING: // Lazy start
    case ACTIVE:
    case STOPPING:
    {
      const CFSRuntimeException* exception = 0;
      try
      {
        d->waitOnOperation(&d->operationLock, "CFSPlugin::uninstall", true);
        if (d->state & (ACTIVE | STARTING))
        {
          exception = d->stop0();
        }
      }
      catch (const CFSException& e)
      {
        // Force to install
        d->setStateInstalled(false);
        d->operationLock.wakeAll();
        exception = new CFSRuntimeException("Stopping plug-in failed", e);
      }
      d->operation.fetchAndStoreOrdered(CFSPluginPrivate::UNINSTALLING);
      if (exception != 0)
      {
        d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), *exception);
        delete exception;
      }
    }
      // Fall through
    case RESOLVED:
    case INSTALLED:
      d->fwCtx->plugins->remove(d->location);
      if (d->operation.fetchAndAddOrdered(0) != CFSPluginPrivate::UNINSTALLING)
      {
        try
        {
          d->waitOnOperation(&d->operationLock, "Plugin::uninstall", true);
          d->operation.fetchAndStoreOrdered(CFSPluginPrivate::UNINSTALLING);
        }
        catch (const CFSPluginException& pe)
        {
          // Make sure that the context is invalid
          if (d->pluginContext != 0)
          {
            d->pluginContext->d_func()->invalidate();
            d->pluginContext.reset();
          }
          d->operation.fetchAndStoreOrdered(CFSPluginPrivate::UNINSTALLING);
          d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), pe);
        }
      }

      d->state = INSTALLED;
      // TODO: use thread
      // bundleThread().bundleChanged(new BundleEvent(BundleEvent.UNRESOLVED, this));
      d->fwCtx->listeners.emitPluginChanged(CFSPluginEvent(CFSPluginEvent::UNRESOLVED, d->q_func()));
      d->cachedHeaders = getHeaders();
      d->pluginActivator = 0;
      d->state = UNINSTALLED;

      // Purge old archive
      if (d->archive)
      {
        d->archive->purge();
      }
      d->operation.fetchAndStoreOrdered(CFSPluginPrivate::IDLE);

      if (d->pluginDir.exists())
      {
        if (!CFS::removeDirRecursively(d->pluginDir.absolutePath()))
        {
          d->fwCtx->listeners.frameworkError(this->d_func()->q_func(), CFSRuntimeException("Failed to delete plugin data"));
        }
        d->pluginDir.setFile("");
      }

      // id, location and headers survive after uninstall.

      // There might be plug-in threads that are running start or stop
      // operations. This will wake them and give them a chance to terminate.
      d->operationLock.wakeAll();
      break;
    }
  }
  d->fwCtx->listeners.emitPluginChanged(CFSPluginEvent(CFSPluginEvent::UNINSTALLED, d->q_ptr));
}

//----------------------------------------------------------------------------
CFSPluginContext* CFSPlugin::getPluginContext() const
{
  //TODO security checks
  Q_D(const CFSPlugin);
  return d->pluginContext.data();
}

//----------------------------------------------------------------------------
long CFSPlugin::getPluginId() const
{
  Q_D(const CFSPlugin);
  return d->id;
}

//----------------------------------------------------------------------------
QString CFSPlugin::getLocation() const
{
  //TODO security
  Q_D(const CFSPlugin);
  return d->location;
}

//----------------------------------------------------------------------------
QHash<QString, QString> CFSPlugin::getHeaders()
{
  //TODO security
  Q_D(CFSPlugin);
  if (d->cachedRawHeaders.empty())
  {
    d->cachedRawHeaders = d->archive->getUnlocalizedAttributes();
  }

  if (d->state == UNINSTALLED)
  {
    return d->cachedHeaders;
  }

  //TODO use the embedded .qm files to localize header values
  return d->cachedRawHeaders;
}

//----------------------------------------------------------------------------
QString CFSPlugin::getSymbolicName() const
{
  Q_D(const CFSPlugin);
  return d->symbolicName;
}

//----------------------------------------------------------------------------
QStringList CFSPlugin::getResourceList(const QString& path) const
{
  Q_D(const CFSPlugin);
  return d->archive->findResourcesPath(path);
}

//----------------------------------------------------------------------------
QStringList CFSPlugin::findResources(const QString& path,
                                     const QString& pattern, bool recurse) const
{
  Q_D(const CFSPlugin);
  return d->findResourceEntries(path, pattern, recurse);
}

//----------------------------------------------------------------------------
QByteArray CFSPlugin::getResource(const QString& path) const
{
  Q_D(const CFSPlugin);
  return d->archive->getPluginResource(path);
}

//----------------------------------------------------------------------------
CFSPluginLocalization CFSPlugin::getPluginLocalization(
  const QLocale& locale, const QString& base) const
{
  Q_D(const CFSPlugin);

  // There are five searching candidates possible:
  // base +
  //    "_" + language1 + "_" + country1 + ".qm"
  // or "_" + language1 + ".qm"
  // or "_" + language2 + "_" + country2 + ".qm"
  // or "_" + language2 + ".qm"
  // or ""  + ".qm"
  //
  // Where language1[_country1[_variation1]] is the requested locale,
  // and language2[_country2[_variation2]] is the default locale.

  QChar localeSep('_');
  QChar baseSep('_');

  QStringList searchCandidates;

  QStringList localeParts = locale.name().split(localeSep);
  QStringList defaultParts = QLocale().name().split(localeSep);

  searchCandidates << baseSep + localeParts[0] + localeSep + localeParts[1];
  searchCandidates << baseSep + localeParts[0];
  searchCandidates << baseSep + defaultParts[0] + localeSep + defaultParts[1];
  searchCandidates << baseSep + defaultParts[0];
  searchCandidates << "";

  QString localizationPath = base.left(base.lastIndexOf('/'));
  QStringList resourceList = this->getResourceList(localizationPath);

  foreach(QString resource, resourceList)
  {
    foreach(QString candidate, searchCandidates)
    {
      if (resource.endsWith(candidate + ".qm"))
      {
        return CFSPluginLocalization(localizationPath + '/' + resource, locale, d->q_ptr);
      }
    }
  }

  return CFSPluginLocalization();
}

//----------------------------------------------------------------------------
CFSVersion CFSPlugin::getVersion() const
{
  Q_D(const CFSPlugin);
  return d->version;
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug debug, CFSPlugin::State state)
{
  switch (state)
  {
  case CFSPlugin::UNINSTALLED:
    return debug << "UNINSTALLED";
  case CFSPlugin::INSTALLED:
    return debug << "INSTALLED";
  case CFSPlugin::RESOLVED:
    return debug << "RESOLVED";
  case CFSPlugin::STARTING:
    return debug << "STARTING";
  case CFSPlugin::STOPPING:
    return debug << "STOPPING";
  case CFSPlugin::ACTIVE:
    return debug << "ACTIVE";
  default:
    return debug << "unknown";
  }
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug debug, const CFSPlugin& plugin)
{
  debug.nospace() << "CFSPlugin[" << "id=" << plugin.getPluginId() <<
      ", state=" << plugin.getState() << ", loc=" << plugin.getLocation() <<
      ", symName=" << plugin.getSymbolicName() << "]";
  return debug.maybeSpace();
}

//----------------------------------------------------------------------------
QDebug operator<<(QDebug debug, CFSPlugin const * plugin)
{
  return operator<<(debug, *plugin);
}

//----------------------------------------------------------------------------
CFSLogStream& operator<<(CFSLogStream& stream, CFSPlugin const * plugin)
{
  stream << plugin->getSymbolicName();
  return stream;
}

//----------------------------------------------------------------------------
CFSLogStream& operator<<(CFSLogStream& stream, const QSharedPointer<CFSPlugin>& plugin)
{
  return operator<<(stream, plugin.data());
}
